#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>

#include "proto.h"

int main(int argc, char *argv[])
{

	int sockfd;
	int size;
	struct msg_st *sbuf;
	struct sockaddr_in raddr;

	if(argc < 3){
		fprintf(stderr, "Usage ./sender [ip] [name]\n");
		exit(1);
	}

	if(strlen(argv[2]) > NAMESIZE){
		fprintf(stderr, "Name is too long...\n");
		exit(1);
	}

	/*
	 * 根据传输数据的大小，动态的申请结构体的的大小
	 * */
	size = sizeof(struct msg_st) + strlen(argv[2]) - 1;
	sbuf = malloc(size);
	if(sbuf == NULL){
		perror("malloc()");
		exit(1);
	}

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if(sockfd < 0){
		perror("socket()");
		exit(1);
	}

	strcpy(sbuf->name,argv[2]);
	sbuf->math = htonl(rand()%100);
	sbuf->chinese = htonl(rand()%100);

	raddr.sin_family = AF_INET;
	raddr.sin_port = htons(atoi(RECPORT));
	inet_pton(AF_INET, argv[1], &raddr.sin_addr);

	if(sendto(sockfd, sbuf, size, 0, (void *)&raddr, sizeof(raddr)) < 0){
		perror("sendto()");
		exit(1);
	}

	puts("OK!");

	close(sockfd);

	exit(1);
}
